package com.chaos.framework.web.interceptor.handler;

import com.chaos.framework.web.annotation.AccessLimit;
import com.chaos.framework.web.annotation.AccessTotalLimit;
import com.chaos.framework.web.service.AccessLimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Component
public class AccessLimitLoginHandler {
    @Autowired
    private AccessLimitService accessLimitService;

    public void handler(HttpServletRequest httpServletRequest, Method method) {
        if (method.isAnnotationPresent(AccessLimit.class)) {
            AccessLimit accessLimit = method.getAnnotation(AccessLimit.class);
            accessLimitService.handleAccessLimit(accessLimit.seconds(), accessLimit.maxCount(), httpServletRequest);
        }
        if (method.isAnnotationPresent(AccessTotalLimit.class)) {
            AccessTotalLimit accessTotalLimit = method.getAnnotation(AccessTotalLimit.class);
            accessLimitService.handleAccessTotalLimit(accessTotalLimit.seconds(), accessTotalLimit.maxCount(), httpServletRequest);
        }
    }
}
